package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;


    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Page<Menu> selectByCondition(String queryString) {
        return null;
    }

    @Override
    //删除权限
    public void deleteById(Integer id) {
        long count = menuDao.findCountByMenuId(id);
        if(count>0){
            throw new RuntimeException("当前检查项被引用不能被删除");
        }
        menuDao.deleteById(id);
    }

    @Override
    public long findCountByMenuId(Integer menuId) {
        return 0;
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.selectByCondition(queryString);
        //查询子类菜单
        //List<Menu> children = menuDao.selectByMenuId();

        List<Menu> list = page.getResult();

        for (Menu menu : list) {
            Integer id = menu.getId();
            List<Menu> menus = menuDao.findByMenuId(id);
            menu.setChildren(menus);
        }

        return new PageResult(page.getTotal(),list);
    }
}
